package database.common;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public abstract class ConnectionFactory {

    public static synchronized Connection getConnection(){
        InitialContext ctx = null;
        Connection conn = null;
        try {
            ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/unizkm");
            conn = ds.getConnection();
        } catch (NamingException e) {
            System.err.println("ConnectionFactory NamingException DataSource lookup");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("ConnectionFactory SQLException while getting Connection");
            e.printStackTrace();
        } catch (Exception ex){
            System.err.println("ConnectionFactory unknown Exception");
            ex.printStackTrace();
        }
        return conn;
    }

}
